﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace SemenQuest.SubWindows
{
    public partial class Settings : UserControl
    {
        private enum SettingTypes
        {
            MinimumValue,
            MaximumValue,
            Value,
            DefaultValue,
        }
        private IEnumerable<Control> GetAll(Control control, Type type)
        {
            var controls = control.Controls.Cast<Control>();
            return controls
                .SelectMany(ctrl => GetAll(ctrl, type))
                .Concat(controls)
                .Where(c => c.GetType() == type);
        }
        public Settings()
        {
            InitializeComponent();

            this.SetStyle(
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.UserPaint,
                true);
            this.UpdateStyles();

            //load settings of sliders
            if (!DesignMode)
            {
                foreach (SettingSlider slider in GetAll(this, typeof(SettingSlider)))
                {
                    LoadSetting(slider.Name, SettingTypes.DefaultValue,
                         Properties.Settings.Default.Properties[slider.Name].DefaultValue);
                    LoadSetting(slider.Name, SettingTypes.Value,
                        Properties.Settings.Default.GetType()
                        .GetProperty(slider.Name)
                        .GetValue(Properties.Settings.Default));
                }
            }

            //load color buttons
            if (!DesignMode)
            {
                foreach (ColorButton button in GetAll(this, typeof(ColorButton)))
                {
                    button.CurrentColor = (Color)Properties.Settings.Default.GetType()
                        .GetProperty(button.SettingName)
                        .GetValue(Properties.Settings.Default);
                    button.DefaultColor = ImageUtils.TranslateString((String)Properties.Settings.Default.Properties[button.SettingName].DefaultValue);
                }
            }
        }

        #region Not used
        //Method to hide an automatic scrollbar
        //ShowScrollBar(Semen.Handle, SB_Direction.SB_HORZ, false);
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern Boolean ShowScrollBar(IntPtr hWnd, SB_Direction wBar, Boolean bShow);
        private enum SB_Direction
        {
            SB_HORZ = 0,
            SB_VERT = 1,
            SB_CTL = 2,
            SB_Both = 3,
        }
        #endregion

        private void LoadSetting(String name, SettingTypes SettingType, Object value)
        {
            Control[] controls = Controls.Find(name, true);
            if (controls.Length > 0)
            {
                if (controls[0].GetType() == typeof(SettingSlider))
                {
                    switch (SettingType)
                    {
                        case SettingTypes.MinimumValue:
                            ((SettingSlider)controls[0]).Minimum = Convert.ToInt32(value);
                            break;
                        case SettingTypes.MaximumValue:
                            ((SettingSlider)controls[0]).Maximum = Convert.ToInt32(value);
                            break;
                        case SettingTypes.Value:
                            ((SettingSlider)controls[0]).Value = Convert.ToInt32(value);
                            break;
                        case SettingTypes.DefaultValue:
                            ((SettingSlider)controls[0]).DefaultValue = Convert.ToInt32(value);
                            break;
                        default:
                            throw new NotSupportedException("Setting type not supported");
                    }
                }
            }
        }
        private void Setting_ValueChanged(Control sender, Int32 value)
        {
            Btn_Save.Enabled = true;
            System.Reflection.PropertyInfo property = 
                Properties.Settings.Default.GetType().GetProperty(sender.Name);
            if (property != null)
            {
                property.SetValue(Properties.Settings.Default, value);
            }
        }
        private void Setting_ColorChanged(String setting, Color color)
        {
            Btn_Save.Enabled = true;
            var property = Properties.Settings.Default.GetType().GetProperty(setting);
            if (property != null)
            {
                property.SetValue(Properties.Settings.Default, color);
            }
        }


        private void ShowHideBtn_Click(Object sender, EventArgs e)
        {
            Tabs.Visible = !Tabs.Visible;
            Btn_Save.Visible = Tabs.Visible;
            Btn_Load.Visible = Tabs.Visible;
            Btn_Reset.Visible = Tabs.Visible;
            ShowHideBtn.ArrowDirection = Tabs.Visible ?
                VerticalTextButton.ArrowDirections.Left :
                VerticalTextButton.ArrowDirections.Right;

        }
        private void Btn_Save_Click(Object sender, EventArgs e)
        {
            Properties.Settings.Default.Save();
            Btn_Save.Enabled = false;
        }
        private void Btn_Reset_Click(Object sender, EventArgs e)
        {
            var result = MessageBox.Show("Are you sure you want to reset all settings to defaults?\r\nCurrent changes will be lost!", "Reset Settings", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
            if (result == DialogResult.OK)
            {
                Properties.Settings.Default.Reset();
                Btn_Save.Enabled = false;
            }
        }
        private void Btn_Load_Click(Object sender, EventArgs e)
        {
            var result = MessageBox.Show("Are you sure you want to load?\r\nCurrent changes will be lost!", "Load Settings", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
            if (result == DialogResult.OK)
            {
                Properties.Settings.Default.Reload();
                Btn_Save.Enabled = false;
            }
        }

        private void Debug_Boid_CheckedChanged(Object sender, EventArgs e)
        {
            Properties.Settings.Default.DebugShowBoidAdjust = Debug_Boid.Checked;
            Btn_Save.Enabled = true;
        }
    }
}
